package com.iot08.api.service;

import com.aliyun.tea.utils.StringUtils;
import com.iot08.api.vo.request.GoldCoinBuyerOrderQueryReqVo;
import com.iot08.common.constant.GoldcoinOrderTypeEnum;
import com.iot08.common.entity.app.GoldCoinBuyerOrder;
import com.iot08.common.entity.app.GoldCoinBuyerServerRecord;
import com.iot08.common.entity.app.SmsNotifyServer;
import com.iot08.common.entity.app.Wallpaper;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @author cwj
 * @description 金币购买服务的订单信息
 * @date 2025/3/13 15:51
 **/
@Slf4j
@Service
public class GoldCoinBuyerOrderService extends BaseService<GoldCoinBuyerOrder> {

    @Resource
    private GoldCoinBuyerServerRecordService goldCoinBuyerServerRecordService;


   /**
    * @description 创建订单信息及订单详情记录信息
    * @author cwj
    * @param [wallpapers, goldCoinTotal, openId, orderStatus, goldCoinBalanceBefore]
    * @return java.lang.String
    * @date @time 2025/3/13 16:37
    **/
    @Transactional
    public String createWallpapperGoldCoinBuyerOrder(List<Wallpaper> wallpapers, Integer costGoldCoinTotal, String openId,
                                                     Integer orderStatus, Integer goldCoinBalanceBefore) {
        //构建金币购买服务的订单信息
        GoldCoinBuyerOrder goldCoinBuyerOrder = buildGoldCoinBuyerOrder(costGoldCoinTotal, openId, orderStatus, goldCoinBalanceBefore);
        //保存订单信息
        goldCoinBuyerOrder = this.save(goldCoinBuyerOrder);
        //创建订单详情信息
        goldCoinBuyerServerRecordService.createWallpapperGoldCoinBuyerServerRecord(wallpapers,openId,goldCoinBuyerOrder.getId(),
                GoldcoinOrderTypeEnum.WALLPAPER.getKey(),GoldcoinOrderTypeEnum.WALLPAPER.getValue(),1,1,"购买壁纸");
        return goldCoinBuyerOrder.getId();
    }

    /**
     * @description 创建购买通知短信服务的订单及订单详情记录
     * @author cwj
     * @param [smsNotifyServer, smsNotifyTime, costGoldCoinTotal, openId, orderStatus, goldCoinBalanceBefore]
     * @return java.lang.String
     * @date @time 2025/3/16 17:47
     **/
    @Transactional
    public String createSmsNotifyGoldCoinBuyerOrder(SmsNotifyServer smsNotifyServer,Integer smsNotifyTime,Integer costGoldCoinTotal,
                                                    String openId, Integer orderStatus, Integer goldCoinBalanceBefore){
        GoldCoinBuyerOrder goldCoinBuyerOrder = buildGoldCoinBuyerOrder(costGoldCoinTotal, openId, orderStatus, goldCoinBalanceBefore);
        //保存订单信息
        goldCoinBuyerOrder = this.save(goldCoinBuyerOrder);
        GoldCoinBuyerServerRecord gcb = goldCoinBuyerServerRecordService.createSmsNotifyGoldCoinBuyerServerRecord(smsNotifyServer,openId,goldCoinBuyerOrder.getId(),
                GoldcoinOrderTypeEnum.SMS_NOTIFY.getKey(),GoldcoinOrderTypeEnum.SMS_NOTIFY.getValue(),1,smsNotifyTime,"购买短信通知服务");
        return !ObjectUtils.isEmpty(gcb) && !StringUtils.isEmpty(gcb.getId()) ? goldCoinBuyerOrder.getId() : null;
    }

    /**
     * @description 构建金币购买服务的订单信息
     * @author cwj
     * @param [goldCoinTotal, openId, orderStatus, goldCoinBalanceBefore]
     * @return com.iot08.common.entity.app.GoldCoinBuyerOrder
     * @date @time 2025/3/13 16:33
     **/
    private GoldCoinBuyerOrder buildGoldCoinBuyerOrder(Integer goldCoinTotal, String openId, Integer orderStatus, Integer goldCoinBalanceBefore) {
        GoldCoinBuyerOrder goldCoinBuyerOrder = new GoldCoinBuyerOrder();
        goldCoinBuyerOrder.setOpenid(openId);
        goldCoinBuyerOrder.setCostGoldCoinTotal(goldCoinTotal);
        goldCoinBuyerOrder.setPaymentTime(DateUtil.dateToStr(new Date(),DateUtil.FORMAT_1));
        goldCoinBuyerOrder.setOrderStatus(orderStatus);
        goldCoinBuyerOrder.setGoldCoinBalanceBefore(goldCoinBalanceBefore);
        goldCoinBuyerOrder.setGoldCoinBalanceAfter(goldCoinBalanceBefore - goldCoinTotal);
        goldCoinBuyerOrder.setCreateTime(new Date());
        goldCoinBuyerOrder.setUpdateTime(new Date());
        return goldCoinBuyerOrder;
    }


    /**
     * @description 查询用户用金币购买服务的订单信息
     * @author cwj
     * @param []
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/27 9:54
     **/
    public ResponseMap queryGoldCoinBuyerOrder(GoldCoinBuyerOrderQueryReqVo vo) throws ParseException {
        return goldCoinBuyerServerRecordService.queryGoldCoinBuyerOrder(vo);
    }


}
